package com.dance.netty.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class MyMessageProtocolDecoder extends ByteToMessageDecoder {
    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
//        System.out.println("自定义协议---->开始解码");
        // 获取定义的边界长度
        int length = in.readInt();
        if(in.readableBytes() >= length){
            // 根据长度读取数据
            byte[] bytes = new byte[length];
            in.readBytes(bytes);
            // 反构造成MessageProtocol
            MessageProtocol messageProtocol = new MessageProtocol(length, bytes);
            out.add(messageProtocol);
//            System.out.println("自定义协议---->解码完成");
        }else{
            // 内容长度不够
        }
    }
}
